package com.myastro.main;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.graphics.Bitmap;

public class LocalFile 
{

	public LocalFile(FrameBuffer framebuffer)
	{
		
		sdf = new SimpleDateFormat("yyyMMdd_HHmmss");
		file = new File("/sdcard/think_BJ");
		
		if (!file.exists()) {
			file.mkdirs();
		}
	}
	
	public void open(int mode)
	{
		long currMs = System.currentTimeMillis();

		// mode 1 : image.
		if (mode == 1) 
		{
			StringBuilder stringbuilder = new StringBuilder().append("capture_");
			Date date = new Date();
			file_name = sdf.format(date);
			file_name = stringbuilder.append(file_name).append(".png").toString();
			
			final File mydir = new File(file, file_name);
			
			try {
				fos = new FileOutputStream(mydir, true);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
						
		} else if (mode == 0){
		// mode 2 : text
			
		} else {
		// default : text
			
		}
	}
	
	public void write(Bitmap bitmap)
	{
		if (fos != null)
		{
			try {
				bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
				
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void read()
	{
		
		
	}
	
	public void close()
	{
		try {
			fos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	private File file = null;
	private FileOutputStream fos = null;
	private SimpleDateFormat sdf = null;
	private String file_name = "";
	
}
